package edu.wtbu.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/addUser")
public class AddUserServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO 自动生成的方法存根
		/**
		 * 1、获取提交的用户信息
		 * 2、建立数据库连接
		 * 3、写入数据
		 * 4、关闭连接
		 */
		
//		解决请求、响应乱码
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
//		获取请求值
//		private String uid;
//		private String userName;
//		private String passWord;
//		private String name;
//		private String email;
//		private String phone;
//		private Date birthday;
//		private String sex;
//		private Integer state;
//		private String code;
//		String uid = req.getParameter("uid");
//		String userName = req.getParameter("userName");
//		String passWord = req.getParameter("passWord");
//		String name = req.getParameter("name");
//		String email = req.getParameter("email");
//		String phone = req.getParameter("phone");
//		String birthday = req.getParameter("birthday");
//		String sex = req.getParameter("sex");
//		String state = req.getParameter("state");
//		Integer code = Integer.parseInt(req.getParameter("code"));
		
		User user = new User("6", "2", "3", "4", "5", "6", null, "7", 8, "9");
		String SQLUrl = "jdbc:mysql://localhost:3306/mmshop?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai";
		String driver = "com.mysql.cj.jdbc.Driver";
		String uname = "root";
		String pwd = "123456";
		try {
//			要求JVM运行时查找并加载driver中的内容 即加载JDBC驱动
			Class.forName(driver);
//			建立连接 使用URL，用户名及密码  DriverManager建立单链接 使用完毕后物理回收
//			与之对应的DdataSources建立连接池，使用完毕后回收连接对象
			Connection connection = DriverManager.getConnection(SQLUrl,uname,pwd);
//			撰写SQL语句
			String sql = "insert into user(uid,username,password,name,email,telephone,sex,state,code) \r\n"
					+ "values('"+user.getUid()+"',"
							+ "'"+user.getUserName()+"',"
							+ "'"+user.getPassWord()+"',"
							+ "'"+user.getName()+"',"
							+ "'"+user.getEmail()+"',"
							+ "'"+user.getPhone()+"',"
							+ "'"+user.getSex()+"',"
							+ "'"+user.getState()+"',"
							+ "'"+user.getCode()+"')";
			
//			prepareStatement指数据库操作命令，将SQL语句发送到数据库 Statement函数不带参数，prepareStatement带参数
			PreparedStatement statement= connection.prepareStatement(sql);
//			执行更新
			int insertResult = statement.executeUpdate();
			if (insertResult>=1) {
				resp.getWriter().write("执行成功");
			}else {
				resp.getWriter().write("执行失败");
			}
			statement.close();
			connection.close();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
		
		
		
	}

}
